#include "MysqlFunc.h"

int SqlConnectionInit(MYSQL **mysql_connect){
    mysql_init(*mysql_connect);
    char *server = "localhost";
    char *user = "root";
    char *password = "123";
    char *database = "NetdiskTest";
    if (!mysql_real_connect(*mysql_connect, server, user, password, database, 0, NULL, 0)) {
        fprintf(stderr, "%s\n", mysql_error(*mysql_connect));
        mysql_close(*mysql_connect);
        return 1;
    }
    return 0;
}

int SqlConnectionClosed(MYSQL **mysql_connect){
    mysql_close(*mysql_connect);
    return 0;
}

int SqlHandler(MYSQL *mysql_connect, char *query, sql_type type, char *result){
    if(mysql_connect == NULL){
        printf("mysql_connect error\n");
    }
    MYSQL_RES *query_res;
    MYSQL_ROW row;
    int query_resul = mysql_query(mysql_connect, query);
    printf("query_resul = %d, query = %s\n", query_resul, query);
    if(query_resul != 0){
        printf("query error %s\n", mysql_error(mysql_connect));
        return 0;
    }
    if(type != rd_sql){
        //不需要结果则直接返回
        return 0; 
    }
    //所有行列都转化为一个用空格隔开的字符串
    query_res = mysql_store_result(mysql_connect);
    unsigned int num_fields = mysql_num_fields(query_res);
    int j = 0;
    while ((row = mysql_fetch_row(query_res))) { // 遍历每一行
        if(j>0){
            strcat(result, " ");
        }
        for(int i = 0; i < num_fields; ++i){ // 遍历每一行中的每一列
            if(i>0){
                strcat(result, " ");
            }
            strcat(result, row[i]);
        }
        j++;
    }
    return 0;
}

int SqlGetPath(MYSQL *mysql_connect, const char *path, int pwd_file_id, char *result_path){
    //判断是相对地址还是绝对地址并返回绝对地址
    //地址不合法result_path返回空

    char path_buf[255] = {0};
    char result[255] = {0};
    char query_str[512] = {0};
    char file_id[8] = {0};
    sprintf(file_id, "%d", pwd_file_id);
    
    int size = strlen(path);
    if(path == NULL || strcmp(path,"")==0||memcmp(path," ",1)==0){
        printf("参数为空\n");
        bzero(query_str, 255);
        sprintf(query_str, "SELECT path FROM virtual_file_system_table WHERE file_id='%s'",file_id);
        SqlHandler(mysql_connect, query_str, rd_sql, path_buf);
        strcat(result, path_buf);
        
        bzero(query_str, 255);
        sprintf(query_str, "SELECT path FROM virtual_file_system_table WHERE path='%s'",result);
        SqlHandler(mysql_connect, query_str, rd_sql, result_path);
        return 0;
    }
    if(path[0] == '/'){
        //如果是绝对路径，去掉/后直接查有没有并返回
        printf("参数为/\n");
        bzero(query_str, 255);
        char user_name_char[21] = {0};
        sprintf(query_str, "SELECT user_name FROM virtual_file_system_table WHERE file_id='%s'",file_id);
        SqlHandler(mysql_connect, query_str, rd_sql, user_name_char);
        strcat(result,user_name_char);

        bzero(query_str, 255);
        if(size >1)
        {
            strcat(result,"/");
            strcat(result,&path[1]);
            
        }
        sprintf(query_str, "SELECT path FROM virtual_file_system_table WHERE path='%s'",result);
        SqlHandler(mysql_connect, query_str, rd_sql, result_path);
        return 0;
    }
    
    //.与正常相同，..则取出当前pwd_file_id的p_id的临时作为pwd_file_id
    if(size >= 2 && path[0] == '.' && path[1] == '.'){
        printf("参数为..\n");
        bzero(query_str, 255);
        sprintf(query_str, "SELECT parent_id FROM virtual_file_system_table WHERE file_id='%s'",file_id);
        bzero(file_id,sizeof(file_id));
        SqlHandler(mysql_connect, query_str, rd_sql, file_id);
        if(strlen(file_id) > 2 && memcmp(file_id,"-1",2) == 0)
        {
            return 0;
        }
        //拼接id获得的绝对路径
        bzero(query_str, 255);
        sprintf(query_str, "SELECT path FROM virtual_file_system_table WHERE file_id='%s'",file_id);
        SqlHandler(mysql_connect, query_str, rd_sql, path_buf);
        strcat(result, path_buf);
        strcat(result, &path[2]);
        //合法检验
        sprintf(query_str, "SELECT path FROM virtual_file_system_table WHERE path='%s'",result);
        SqlHandler(mysql_connect, query_str, rd_sql, result_path);
        return 0;
    }
    
    if(size >= 1 && path[0] == '.'){
        //拼接id获得的绝对路径
        printf("参数为.\n");
        bzero(query_str, 255);
        sprintf(query_str, "SELECT path FROM virtual_file_system_table WHERE file_id='%s'",file_id);
        SqlHandler(mysql_connect, query_str, rd_sql, path_buf);
        strcat(result, path_buf);
        strcat(result, &path[1]);
        //合法检验
        sprintf(query_str, "SELECT path FROM virtual_file_system_table WHERE path='%s'",result);
        SqlHandler(mysql_connect, query_str, rd_sql, result_path);
        return 0;
    }
    //当前目录
    printf("当前目录\n");
    bzero(query_str, 255);
    sprintf(query_str, "SELECT path FROM virtual_file_system_table WHERE file_id='%s'",file_id);
    SqlHandler(mysql_connect, query_str, rd_sql, path_buf);
    strcat(result, path_buf);
    strcat(result,"/");
    strcat(result, path);
    //合法检验
    sprintf(query_str, "SELECT path FROM virtual_file_system_table WHERE path='%s'",result);
    SqlHandler(mysql_connect, query_str, rd_sql, result_path);
    return 0;
}

int SqlAddLog(MYSQL *mysql_connect, char *priority, char *msg, char *func){
    char sql[255] = {0};
    bzero(sql,strlen(sql));
    char priority_temp[64] = {0};
    if(priority == NULL){
        strcpy(priority_temp, "INFO");
    }else{
        strcpy(priority_temp, "priority");
    }
    time_t now;
    now = time(NULL);
    struct tm *local = localtime(&now);
    char *time_char = asctime(local);
    time_char[strlen(time_char) - 1] = '\0';

    sprintf(sql, \
    "insert into log_table(priority, msg, time, func) values('%s','%s','%s','%s');"\
    , priority_temp, msg, time_char, func);
    SqlHandler(mysql_connect, sql, wr_sql, NULL);
    return 0;
}
